﻿// PRD.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <random>
#include <array>
#include <algorithm>
#include <vector>
#include <iterator>
#include <numeric>
#include <sstream>
#include "pseudo_random_real_distribution.hpp"
using namespace std;

using pseudo_random_distribution = pseudo_random_real_distribution<>;

int main()
{
	random_device rd;
	default_random_engine engine(rd());
	constexpr double P = 0.15;
	pseudo_random_distribution PRD(P);

	array<bool, 1'000'000> test;

	generate(test.begin(), test.end(), [&]
	{
		return PRD(engine);
	});

	const double averageP = count(test.begin(), test.end(), true) * 1. / test.size();

	vector<size_t> vecN;
	auto it1 = test.begin();
	auto it2 = test.end();
	do
	{
		auto it2 = find(it1, test.end(), true);
		if (it2 == test.end())
			break;
		vecN.emplace_back(distance(it1, it2) + 1);
		it1 = next(it2);
	} while (it1 != test.end());
	const size_t nMax = vecN.empty() ? 0 : *max_element(vecN.begin(), vecN.end());

	const double averageN = accumulate(vecN.begin(), vecN.end(), 0.) / vecN.size();

	stringstream ss;
	ss << PRD;

	pseudo_random_distribution otherPRD;
	ss >> otherPRD;

	cout << "P为" << P << '\n';
	cout << "伪随机常数C为" << PRD.C() << '\n';
	cout << "伪随机常数最大N值为" << 向上取整(1., PRD.C()) << "\n\n";

	cout << "实测伪随机平均概率为" << averageP << '\n';
	cout << "实测伪随机最大N值为" << nMax << '\n';
	cout << "实测伪随机平均N值为" << averageN << '\n';
	cout << "流保存后是否相等：" << boolalpha << (PRD == otherPRD) << '\n';
}
